package com.gaga.console.base.utils;

import cn.hutool.core.util.StrUtil;
import com.gaga.console.core.entity.SysDatabase;
import com.gaga.metadata.common.enums.TransFormType;

/**
 * 用于database相关工具类
 *
 * @author liujia
 * @version 1.0
 * @date 2023/3/7 9:45
 */
public class DataBaseUtils {

    /**
     * 通过Datasource 获取url
     *
     * @param datasource
     * @param schema
     * @return
     */
    public static String getUrlByDatasource(SysDatabase datasource, String schema) {

        StringBuffer url = new StringBuffer();
        switch (TransFormType.valueOf(StrUtil.upperFirst(datasource.getType()))) {
            case Greenplum:
                url.append(TransFormType.Greenplum.getUrlPrefix())
                        .append(datasource.getIp()).append(":")
                        .append(datasource.getPort())
                        .append("/").append(datasource.getName())
                        .append("?currentSchema=").append(schema);
                break;
            case Postgres:
                url.append(TransFormType.Postgres.getUrlPrefix())
                        .append(datasource.getIp()).append(":")
                        .append(datasource.getPort())
                        .append("/").append(datasource.getName())
                        .append("?currentSchema=").append(schema);
                break;
            case Postgresql:
                url.append(TransFormType.Postgresql.getUrlPrefix())
                        .append(datasource.getIp()).append(":")
                        .append(datasource.getPort())
                        .append("/").append(datasource.getName()).append("?currentSchema=").append(schema);
                break;
            case StarRocks:
                url.append(TransFormType.StarRocks.getUrlPrefix())
                        .append(datasource.getIp()).append(":")
                        .append(datasource.getPort())
                        .append("/").append(datasource.getName())
                        .append("?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai");
                break;
            case Doris:
                url.append(TransFormType.Doris.getUrlPrefix())
                        .append(datasource.getIp()).append(":")
                        .append(datasource.getPort())
                        .append("/").append(datasource.getName())
                        .append("?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai");
                break;
            case Sqlserver:
                url.append(TransFormType.Sqlserver.getUrlPrefix())
                        .append(datasource.getIp()).append(":")
                        .append(datasource.getPort())
                        .append(";DatabaseName=").append(datasource.getName());
                break;
//            case Oracle:
//                String serviceType = StringUtils.isNotBlank(datasource.getServiceType()) ? ServiceType.getByCode(datasource.getServiceType()) : ServiceType.SID_SERVICE.getInfo();
//                url.append(TransFormType.Oracle.getUrlPrefix())
//                        .append(datasource.getIp()).append(":")
//                        .append(datasource.getPort())
//                        .append(serviceType).append(datasource.getName());
//                break;
            case Mysql:
                url.append(TransFormType.Mysql.getUrlPrefix())
                        .append(datasource.getIp()).append(":")
                        .append(datasource.getPort())
                        .append("/").append(datasource.getName())
                        .append("?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai");
                break;
            default:
                break;
        }
        return url.toString();
    }
}


